<template>
  <div>
    <div style="margin-top: 2%;text-align:center;">
      <el-select  v-model="form.academyId"
                  style="width: 18%;margin-right: 1%"
                  clearable filterable
                  placeholder="请选择学院"
                  @change="professionInit(form.academyId)">
        <el-option v-for="item in academy" :key="item.academyId"
                   :label="item.academyId +' - '+ item.academyName"
                   :value="item.academyId">
        </el-option>
      </el-select>
      <el-select  v-model="form.professionId"
                  style="width: 18%;margin-right: 1%"
                  clearable filterable
                  placeholder="请选择专业">
        <el-option v-for="item in profession" :key="item.professionId"
                   :label="item.professionName" :value="item.professionId">
        </el-option>
      </el-select>
      <el-input style="width: 18%;margin-right: 1%" placeholder="请输入待查找的班级"
                v-model="form.clazzId" oninput ="value=value.replace(/[^0-9]/g,'')"></el-input>
      <el-button @click="findClazz()" style="font-size: 20px" type="primary">立即查找</el-button>
      <el-button @click="addClazz()" style="font-size: 20px" type="primary">点击此处新增班级</el-button>
    </div>

    <template style="margin-top: 2%">
      <el-table :data="pageInfo.list" style="width: 100%">
        <el-table-column label="" width="150">
        </el-table-column>
        <el-table-column label="班级" width="300" prop="clazzId" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.clazzId}}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属专业" width="600" prop="professionName" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.professionName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属学院" width="400" prop="academyId" sortable>
          <template slot-scope="scope">
            <span>{{scope.row.academyName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="small"
                style="background-color: deepskyblue;font-size: 16px"
                @click="handleEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-bottom: 1%;"></div>
      <el-pagination
          style="text-align: center;"
          background
          layout="total,prev, pager, next"
          :total="pageInfo.total"
          :page-size="pageInfo.pageSize"
          @current-change="handleCurrentChange">
      </el-pagination>
    </template>

    <el-drawer title="更新班级" :visible.sync="modify" :direction="modifyDirection" :before-close="handleClose">
      <el-card style="margin:0 auto;width: 96%;">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="20%">
          <el-form-item  label="班级编号" prop="clazzId">
            <el-input style="width: 60%;"
                      placeholder="请输入新的班级编号"
                      v-model="ruleForm.clazzId"
                      oninput ="value=value.replace(/[^0-9]/g,'')"></el-input>
          </el-form-item>
          <el-form-item label="选择学院">
            <el-select  v-model="ruleForm.academyId"
                        style="width: 60%;"
                        clearable filterable
                        placeholder="请选择学院"
                        @change="professionInit(ruleForm.academyId)">
              <el-option v-for="item in academy" :key="item.academyId"
                         :label="item.academyId +' - '+ item.academyName"
                         :value="item.academyId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="选择专业" prop="professionId">
            <el-select  v-model="ruleForm.professionId"
                        style="width: 60%;"
                        clearable filterable
                        placeholder="请选择专业(请先选择学院)">
              <el-option v-for="item in profession" :key="item.professionId"
                         :label="item.professionName" :value="item.professionId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即提交</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-drawer>
  </div>
</template>

<script>
export default {
  data(){
    return {
      modify: false,
      modifyDirection: 'rtl',
      form:{
        academyId:'',
        professionId:'',
        clazzId:'',
      },
      academy:[],
      profession:[],
      currPage: 1,
      pageInfo:{},
      ruleForm:{
        oldClazzId: '',
        clazzId: '',
        academyId:'',
        professionId: '',
      },
      rules: {
        clazzId: [
          { required: true, message: '请输入班级编号', trigger: 'blur' },
          { min: 2, max: 8, message: '请输入2 - 8位数字!', trigger: 'blur' }
        ],
        professionId: [
          { required: true, message: '请选择专业', trigger: 'blur' },
        ]
      }
    }
  },
  created() {
    this.academyInit();
    this.getData();
  },
  methods:{
    submitForm(formName){ // 新增/修改班级
      this.$refs[formName].validate((valid) => {//校验格式
        if (valid) {//格式校验通过
          let path = `http://localhost:8081/clazz/updateClazz/${this.ruleForm.oldClazzId}`;
          this.$ajax.post(path,this.ruleForm).then(res=>{
            if(res.data.status === 0){
              this.$message.success(res.data.message);
              this.modify = false;
              this.getData();
            }
            else{
              this.$message.error(res.data.message);
            }
          })
        }
      });
    },
    handleEdit(row){
      this.ruleForm.oldClazzId = row.clazzId;
      this.ruleForm.clazzId = row.clazzId + '';
      this.ruleForm.academyId = row.academyId;
      this.professionInit(row.academyId);
      this.ruleForm.professionId = row.professionId;
      this.modify = true;
    },
    addClazz(){
      this.ruleForm.oldClazzId = -1;
      this.ruleForm.clazzId = '';
      this.ruleForm.academyId = '';
      this.professionInit('');
      this.ruleForm.professionId = '';
      this.modify = true;
    },
    academyInit(){//初始化学院
      let path = 'http://localhost:8081/academy/selectAcademyCanUse';
      this.$ajax.post(path).then(res=>{
        this.academy = res.data.list
      })
    },
    professionInit(val){//根据学院初始化专业
      this.form.professionId = '';
      this.ruleForm.professionId = '';
      if(val !== ''){
        let path = `http://localhost:8081/profession/getProfession/${val}`;
        this.$ajax.post(path).then(res=>{
          this.profession = res.data.list
        })
      }
      else{
        this.profession = null;
      }
    },
    findClazz(){//请求数据
      this.currPage = 1;
      this.getData();
    },
    handleCurrentChange(val){//翻页
      this.currPage = val;
      this.getData();
    },
    getData(){
      let path = `http://localhost:8081/clazz/getClazzList/${this.currPage}`;
      this.$ajax.post(path,this.form).then(res=>{
        this.pageInfo = res.data;
      })
    },
    handleClose(done){
      this.professionInit(this.form.academyId);
      done();
    }
  }
}
</script>

<style scoped>
span{
  font-size: 18px;
}
</style>